package com.powernode.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @TableName sys_user
 */
@TableName(value = "sys_user")
@Data
public class SysLoginUser implements Serializable, UserDetails {
    private Long userId;

    private String username;

    private String password;

    private static final long serialVersionUID = 1L;
    //表中没有对应的列
    @TableField(exist = false)
    private List<String> permList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> list = new LinkedList<>();
        this.permList.forEach(perm -> {
            if (perm.contains(",")) {
                List<SimpleGrantedAuthority> currentList = Stream.of(perm.split(","))
                        .map(str -> new SimpleGrantedAuthority(str))
                        .collect(Collectors.toList());
                list.addAll(currentList);
            } else {
                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(perm);
                list.add(simpleGrantedAuthority);
            }
        });
        return list;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
